package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.Teacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.mapper.TeacherMapper;
import com.atguigu.eduservice.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author ran
 * @since 2022-01-25
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Map pageQuery(Page<Teacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByAsc("sort");
        HashMap<String, Object> hashMap = new HashMap<>();
        if (teacherQuery == null) {
            IPage<Teacher> teacherIPage = baseMapper.selectPage(pageParam, null);
            List<Teacher> teachers = teacherIPage.getRecords();
            long total = teacherIPage.getTotal();
            hashMap.put("items",teachers);
            hashMap.put("total",total);
            return hashMap;
        }
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        IPage<Teacher> teacherIPage2 = baseMapper.selectPage(pageParam, queryWrapper);
        List<Teacher> teachers = teacherIPage2.getRecords();
        System.out.println(teachers.toString() + "--------------------------");
        long total = teacherIPage2.getTotal();
        hashMap.put("items",teachers);
        hashMap.put("total",total);
        return hashMap;
    }

}

